/**
 *  @author Morozan Ion
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.dddforandroid.util;

import java.io.IOException;
import java.util.logging.Logger;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;

import com.dddforandroid.c2dm.AuthenticationUtil;
import com.dddforandroid.classes.C2DMConfig;
import com.dddforandroid.crud.PMF;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
/**
 * Useful methods and constants 
 *
 */
public class Util {

	public static final String USER = "c2dmreg@gmail.com";
	public static final String PASSWORD = "test123.";

	public final static String DEVICE_ID = "deviceid";
	public final static String REGSITRATION_ID = "registrationid";

	public final static String ADMIN_NAMESPACE = "admin";

	public final static long key = 1;

	public Util() {

	}

	/**
	 * Get information about the logged user
	 * 
	 * @return current logged in user id
	 */
	public String getUserId() {

		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		return user.getUserId();
	}

	/**
	 * Get information about the logged user
	 * 
	 * @return current logged in user email
	 */
	public String getUserEmail() {

		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		return user.getEmail();
	}

	/**
	 * Update the token.
	 * 
	 * Called on "Update-Client-Auth" or when admins set a new token.
	 * 
	 * @param token
	 */
	public static void updateToken(String token) {
		if (token != null) {
			PersistenceManager db = PMF.get().getPersistenceManager();

			C2DMConfig c2dmInfo = new C2DMConfig(token);
			c2dmInfo.setKey(key);
			try {
				db.makePersistent(c2dmInfo);
			} finally {
				db.close();
			}
		}
	}

	/**
	 * Return the auth token.
	 * 
	 * It'll first memcache, if not found will use the database.
	 * 
	 * @return
	 * @throws IOException
	 */
	public static String getToken() throws IOException {
		String auth_token = null;
		PersistenceManager db = PMF.get().getPersistenceManager();
		try {
			try {
				auth_token = db.getObjectById(C2DMConfig.class, key)
						.getAuthToken();
			} catch (JDOObjectNotFoundException e) {
				/**
				 * if the token is not present - get a token(this can happen
				 * only first time)
				 * */
				auth_token = AuthenticationUtil.getTokenFromServer(Util.USER,
						Util.PASSWORD);
				Logger.getLogger("Auth token not found so invoke the request");
			}
		} finally {
			db.close();
		}
		return auth_token;
	}

}
